<?php
namespace Home\Controller;
use Think\Controller;
class SearchController extends CommonController{

	//显示搜索结果界面
	public  function result(){
		$key = I("get.key");

		$Goods = M("Goods");
		$map['goodsname'] = array('like','%'.$key.'%');
		
		//分页输出搜索结果
		$resnum =  $Goods->where($map)->count();
		$Page    = new \Think\Page($resnum,1);// 实例化分页类 传入总记录数和每页显示的记录数(3)
		$Page->setConfig('header',"<span>共<b>%TOTAL_ROW%</b>条记录 第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</span>");
		$Page->setConfig('first','首页');
		$Page->setConfig('last','共%TOTAL_PAGE%页');
		$Page->setConfig('prev','<<');
		$Page->setConfig('next','>>');
		$Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
		$show  = $Page->show();// 分页显示输出

		$searchres = $Goods->where($map)->limit($Page->firstRow.','.$Page->listRows)->select();


		if (!empty($searchres)) {
			$searchgoodslist = array();
			foreach ($searchres as $value) {
				$value['goodsimg'] = json_decode($value['goodsimg'],true);
				$searchgoodslist[] = $value;
			}

			$this->assign("searchgoodslist",$searchgoodslist);
		}

		$this->assign("key",$key);
		$this->assign("resnum",$resnum);
		$this->assign("show",$show);
		$this->display("search");
	}
	
}